package cn.car.biz.impl;

import cn.car.biz.VehicleEntryExitInfoBiz;
import cn.car.biz.carAccess.event.VehicleChangeEvent;
import cn.car.domains.entity.VehicleCommonInfoEntity;
import cn.car.domains.entity.VehicleEntryExitInfoEntity;
import cn.car.domains.enums.UploadGovStatusEnum;
import cn.car.domains.vo.vehicleEntryExit.CreateEntryExitInfoReq;
import cn.car.domains.vo.vehicleEntryExit.UpdateVehicleEntryExitReq;
import cn.car.domains.vo.vehicleEntryExit.VehicleEntryExitInfoRes;
import cn.car.exception.CarAccessControlException;
import cn.car.service.VehicleCommonInfoService;
import cn.car.service.VehicleEntryExitInfoService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class VehicleEntryExitInfoBizImpl implements VehicleEntryExitInfoBiz {


    @Resource
    private VehicleEntryExitInfoService vehicleEntryExitInfoService;

    @Resource
    private VehicleCommonInfoService vehicleCommonInfoService;

    @Resource
    private ApplicationEventPublisher publisher;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createVehicle(CreateEntryExitInfoReq req) {
        // 1. 请求参数VO -> 数据库实体Entity
        VehicleEntryExitInfoEntity vehicleEntity = new VehicleEntryExitInfoEntity();
        BeanUtils.copyProperties(req, vehicleEntity);

        // 2. 补充VO未包含的默认值（若服务层未做自动填充）
        vehicleEntity.setGovUploadStatus(UploadGovStatusEnum.INIT.name()); // 默认启用车辆

        vehicleEntryExitInfoService.insert(vehicleEntity);

        VehicleCommonInfoEntity vehicleCommonInfoEntity = new VehicleCommonInfoEntity();
        BeanUtils.copyProperties(req, vehicleCommonInfoEntity);

        vehicleCommonInfoEntity.setEnabled(true);

        vehicleCommonInfoEntity.setAssociationId(vehicleEntity.getId());

        vehicleCommonInfoService.insert(vehicleCommonInfoEntity);

        //现在车辆入库 发送车辆入库的消息
        VehicleChangeEvent createEntryExitEvent = new VehicleChangeEvent(this, vehicleEntity.getId());
        publisher.publishEvent(createEntryExitEvent);

    }

    @Override
    public VehicleEntryExitInfoRes getVehicleByNumber(String vehicleNumber) {

        VehicleEntryExitInfoRes vehicleEntryExitInfoRes = new VehicleEntryExitInfoRes();

        VehicleEntryExitInfoEntity vehicleEntryExitInfoEntity = vehicleEntryExitInfoService.queryVehicleByNumber(vehicleNumber);

        VehicleCommonInfoEntity vehicleCommonInfoEntity = vehicleCommonInfoService.getVehicleByNumber(vehicleNumber);

        BeanUtils.copyProperties(vehicleEntryExitInfoEntity, vehicleEntryExitInfoRes);

        BeanUtils.copyProperties(vehicleCommonInfoEntity, vehicleEntryExitInfoRes);

        return vehicleEntryExitInfoRes;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateVehicle(UpdateVehicleEntryExitReq req) {
        // 1. 校验：车辆是否存在（避免更新不存在的数据）
        VehicleCommonInfoEntity existingVehicle = vehicleCommonInfoService.getVehicleById(req.getId());
        if (existingVehicle == null) {
            throw CarAccessControlException.DATE_NOT_EXIST.newInstance("车辆不存在，无法更新");
        }

        // 2. 更新请求VO -> 数据库实体Entity
        VehicleCommonInfoEntity updateEntity = new VehicleCommonInfoEntity();
        BeanUtils.copyProperties(req, updateEntity);

        // 3. 调用服务层执行更新
        vehicleCommonInfoService.updateVehicle(updateEntity);


        // 2. 更新请求VO -> 数据库实体Entity
        VehicleEntryExitInfoEntity updateEntryExitEntity = new VehicleEntryExitInfoEntity();
        BeanUtils.copyProperties(req, updateEntryExitEntity);

        // 3. 调用服务层执行更新
        vehicleEntryExitInfoService.update(updateEntryExitEntity);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVehicle(Long id) {
        VehicleCommonInfoEntity existingVehicle = vehicleCommonInfoService.getVehicleById(id);
        if (existingVehicle == null) {
            throw CarAccessControlException.DATE_NOT_EXIST.newInstance("车辆不存在，无法删除");
        }

        // 3. 调用服务层执行删除
        vehicleCommonInfoService.deleteVehicle(id);

        vehicleEntryExitInfoService.deleteVehicle(id);
    }
}
